<?php
namespace Admin\Controller;

class GoodsController extends AdminController {

	#列表
	function index() {	
		$keyword  = I('get.keyword');
		if($keyword!=''){
			$map['name'] = array('like', '%'.$keyword.'%');
		}
		$obj = M('Goods');
		$count = $obj->where($map)->count();
		import("Common.ORG.Page");
		$page = new \Page($count, 20);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('keyword', $keyword);
		$this->display();
	}

	#添加
	public function add() {
		$this->display();
	}

	#保存
	public function save() {
		$this->all_add('Goods',U('Goods/index'));
	}


	#编辑
	public function edit() {
		$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('Parameter error');
		}

		$obj = M('Goods');
		$list = $obj->where("id=$id")->find();
		if (!$list) {
			$this->error('Information does not exist');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}


	#更新
	public function update() {
		$this->all_save('Goods',U('Goods/index'));
	}

	#删除
	public function del() {
		$id=I('post.selectedids');
		if($id==''){
			$state = "You do not select any information!";
		}else{
			foreach ($id as $v) {
				$obj = M('Goods');
				$where['id'] = $v;
				$list = $obj->field('picname')->where($where)->find();
				if ($list['picname']!='') {
					unlink(C('SITE_PATH').$list['picname']);
				}
				$obj->where($where)->delete();
			}
			$state = 'SUCCESS';
	        $message = 'Successful operation';
	        $url = U('Goods/index');
		}
		echo $this->echo_json_str($state,$message,$url);
	}	
}
?>